<?php get_header(); ?>
<div id="list_container" class="grid_11 fright">
<?php
$query = pods_url_variable('s','get');
$city = pods_url_variable('c','get');

/*
preg_match_all('/"(?:\\\\.|[^\\\\"])*"|\S+/', $query, $matches);
foreach ($matches[0] as $match) {
    $sqlItems[] = "'" . mysql_real_escape_string($match) . "'";
}
$sqlkeys = implode(",", $sqlItems);
printr($sqlItems,1);
*/

preg_match_all('/"(?:\\\\.|[^\\\\"])*"|\S+/', $query, $matches);
foreach($matches[0] as $match){
    $sqlItems .= "(t.name LIKE '%".mysql_real_escape_string($match)."%' OR ";
    $sqlItems .= "t.short LIKE '%".mysql_real_escape_string($match)."%') AND ";
}

$deal = new Pod('deal');
$deal->page = 1;
$deal->search = false; 
//$deal->findRecords('id DESC', -1, "(t.name LIKE '%$query%' OR t.short LIKE '%$query%') AND city.name='$city' AND t.camp_end > ".fix_date());
//$deal->findRecords('id DESC', -1, "MATCH(t.name, t.short) AGAINST('50 off' IN BOOLEAN MODE) AND city.name='$city' AND t.camp_end > ".fix_date());
$deal->findRecords('id DESC', -1, "$sqlItems city.name='$city' AND t.camp_end > ".fix_date());
$total_deals = $deal->getTotalRows();

/*
$localdeals = new Pod('deal');
$localdeals->page = 1;
$localdeals->search = false; 
$localdeals->findRecords('id DESC', -1, "(t.name LIKE '%$query%' OR t.short LIKE '%$query%') AND t.camp_end > ".fix_date()." AND t.sale = 1 ");
$total_local_deals = $localdeals->getTotalRows();
*/

$brand = new Pod('brand');
$brand->page = 1;
$brand->search = false; 
$brand->findRecords('id DESC', -1, "(t.name LIKE '%$query%' OR t.body LIKE '%$query%' OR campaigns.name LIKE '%$query%' OR campaigns.short LIKE '%$query%') AND t.local='0' AND campaigns.camp_end > ".fix_date()."");
$total_brands = $brand->getTotalRows();

$category = new Pod('category');
$category->page = 1;
$category->search = false; 
$category->findRecords('id DESC', -1, "t.name LIKE '%$query%' OR campaigns.name LIKE '%$query%' OR campaigns.short LIKE '%$query%' AND campaigns.camp_end > ".fix_date()."");
$total_categories = $category->getTotalRows();

$tags = new Pod('tags');
$tags->page = 1;
$tags->search = false; 
$tags->findRecords('id DESC', -1, "t.name LIKE '%$query%' OR campaigns.name LIKE '%$query%' OR campaigns.short LIKE '%$query%' AND campaigns.camp_end > ".fix_date()."");
$total_tags = $tags->getTotalRows();

?>

<h1><?php echo stripslashes(stripslashes(ucwords($query))).' Search Results'; ?></h1>
<script type="text/javascript" charset="utf-8">
var pageOptions = {
 'pubId' : 'pub-3655588719499040',
 'query' : '<?php echo $query; ?>'
};

var search_ads = {
 'container' : 'search_ads',
 'number' : 2,
 'width' : '596px',
 'lines' : 2,
 'fontFamily' : 'arial',
 'fontSizeTitle' : '14px',
 'fontSizeDescription' : '14px',
 'fontSizeDomainLink' : '14px',
 'colorBorder' : '#F3F3F3',
 'colorBackground' : '#F3F3F3',
 'colorTitleLink' : '#004B91',
 'colorText' : '#000000',
 'colorDomainLink' : '#004B91',
 'channel' : '4149581344',
 'linkTarget' : '_blank'
};

new google.ads.search.Ads(pageOptions, search_ads);
</script>
<div id="search_ads" class="unit campaign"></div>


<div class="views">
  <a class="grid g-button">Grid View</a>
  <a class="list g-button">List View</a>
</div>
<div class="clear"></div>
<div id="list_wrap">	
<ul class="list">
<?php
if($total_deals > 0){
	echo $deal->showTemplate('list_grid');
}
else{
	echo "<h2>Sorry, there aren't any offers for ".ucwords(urldecode($query))."</h2>";
}
?>
</ul>
</div>
<div class="clear"></div>
</div><!-- end content -->

<div id="sidebars" class="grid_5 right-sidebar">
	<div id="r-sidebar">
		
		<?php if($total_brands > 0){ ?>
		<div class="widget box" id="rel_brands">
			<h2>Related Brands</h2>
			<?php
			while ($brand->fetchRecord())
			{
				$brand_name = $brand->get_field('name');
      			$brand_logo = $brand->get_field('logo');
				$small_image = wp_get_attachment_image_src($brand_logo[0][ID], 'thumbnail', false);
				$small_image = $small_image[0];
				$brand_url =  $brand->get_field('brandurl');
				if(!empty($small_image)){
			?>
					<span style="width:100px;padding:5px;">
						<a href="<?php echo BUZZR_URL.'/brands/'.$brand_url; ?>">
							<img src="<?php echo $small_image; ?>" alt="<?php echo $brand_name.' Offers & Deals'; ?>" />
						</a>
					</span>
			<?php	
				}
			}
			?>
		</div>
		<?php } ?>
		
	</div><!-- end #r-sidebar -->
</div><!-- end #sidebars -->

<?php get_footer(); ?>
